//
//  PCDShareReceipt.m
//  PCDBankSix
//
//  Created by liumingfei on 2022/6/6.
//

#import "PCDShareReceipt.h"
#import "PCDShareModel.h"

@implementation PCDShareReceipt

- (void)handler:(id)data andContext:(PCDContext *)context ResponseCallback:(WVJBResponseCallback )responseCallback {
    NSDictionary *dict = (NSDictionary *)data;
    NSString *strCallback = dict[@"success"];
    NSString *failCallback = dict[@"fail"];
    WKWebView *web =  self.bsContext.webView;
    UIImage *shotcut = [self imageRepresentationWithWebView:web];
    
    PCDShareContent *imgcontent = [[PCDShareContent alloc]init];
    imgcontent.title = @"";
    imgcontent.text = @"";
    imgcontent.image = shotcut;
    imgcontent.shareContentType = PCDShareContentTypeImage;
    
    [PCDShareModel weixinShareWith:imgcontent completionHandler:^(PCDShareResponse *response) {
        if (response.retCode == 1) {
            NSDictionary *dict = @{@"STATUS":@"1", @"MSG":@"分享成功"};
            evaluateJavaScriptOnMainThread(self, strCallback, [dict JSONRepresentation]);
        }else {
            NSDictionary *dict = @{@"STATUS":@"0", @"MSG":response.errorMessage};
            evaluateJavaScriptOnMainThread(self, strCallback, [dict JSONRepresentation]);
        }
    }];
}

- (UIImage *)imageRepresentationWithWebView:(WKWebView *)webView{
    CGFloat scale = [UIScreen mainScreen].scale;
    
    CGSize boundsSize = webView.bounds.size;
    CGFloat boundsWidth = boundsSize.width;
    CGFloat boundsHeight = boundsSize.height;
    
    CGSize contentSize = webView.scrollView.contentSize;
    CGFloat contentHeight = contentSize.height;
    
    CGPoint offset = webView.scrollView.contentOffset;
    
    [webView.scrollView setContentOffset:CGPointMake(0, 0)];
    
    NSMutableArray *images = [NSMutableArray array];
    while (contentHeight > 0) {
        UIGraphicsBeginImageContextWithOptions(boundsSize, NO, 0.0);
        [webView.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        [images addObject:image];
        
        CGFloat offsetY = webView.scrollView.contentOffset.y;
        [webView.scrollView setContentOffset:CGPointMake(0, offsetY + boundsHeight)];
        contentHeight -= boundsHeight;
    }
    
    [webView.scrollView setContentOffset:offset];
    
    CGSize imageSize = CGSizeMake(contentSize.width * scale,
                                  contentSize.height * scale);
    UIGraphicsBeginImageContext(imageSize);
    [images enumerateObjectsUsingBlock:^(UIImage *image, NSUInteger idx, BOOL *stop) {
        [image drawInRect:CGRectMake(0,
                                     scale * boundsHeight * idx,
                                     scale * boundsWidth,
                                     scale * boundsHeight)];
    }];
    UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(fullImage, nil, nil, nil);
    return fullImage;
}



@end
